package com.daqsoft.service.process;

import com.daqsoft.dao.project.ProjectUserDao;
import com.daqsoft.data.Message;
import com.daqsoft.data.User;
import com.daqsoft.data.project.Project;
import com.daqsoft.data.project.ProjectUser;
import com.daqsoft.data.project.Task;
import com.daqsoft.dto.StatusEnum;
import com.daqsoft.dto.TaskTypeEnum;
import com.daqsoft.service.NoticeService;
import com.daqsoft.service.project.ProjectService;
import com.daqsoft.service.project.TaskService;
import com.daqsoft.util.DateUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 项目计划确认流程服务层
 *
 * @author 周麟东 on 9:36
 */
@Service
public class ProjectConfimProcessService {

    private Logger log = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private ProjectService projectService;
    @Autowired
    private ProjectUserDao projectUserDao;
    @Autowired
    private NoticeService noticeService;
    @Autowired
    private TaskService taskService;

    /**
     * 第一步 : 项目经理提交项目, 该操作会执行以下步骤
     * 1. 将项目更改为CHECK状态
     * 2. 为所有负责人添加一条确认任务
     * 3. 保存这些负责人到项目中
     * 4. 通知所有负责人
     *
     * @param project 要提交的项目对象
     */
    @Transactional
    public void submitProject(Project project) {
        //设置项目为check状态
        project.setStatus(StatusEnum.CHECK);
        //为项目中的每个负责人增加一个确认任务
        Map<Integer, User> map = projectService.getAllProjectVerifier(project);
        List<Task> tasks = new ArrayList<>();
        //参与者
        //先删除所有参与者
        List<ProjectUser> projectUsers = projectUserDao.findByProject(project);
        if (projectUsers != null && projectUsers.size() != 0) {
            projectUserDao.deleteByProject(project);
        }

        projectUsers = new ArrayList<>();
        for (User user : map.values()) {
            Task task = new Task();
            task.setStatus(StatusEnum.PROCESS);
            task.setType(TaskTypeEnum.CONFIRM);
            task.setProject(project);
            task.setStartDate(new Date());//这个任务开始时间就是马上开始
            task.setEndDate(DateUtil.INS.addDay(new Date(), 3));//结束时间就是三天后
            task.setVerifier(user);
            task.setPrincipal(user);//这个任务的责任人 和负责人 都是一个人
            task.setName("项目计划确认!");
            task.setMsg("#系统添加计划确认任务#");
            tasks.add(task);
            //将参与者增加到项目中
            ProjectUser projectUser = new ProjectUser();
            projectUser.setConfirm(false);
            projectUser.setProject(project);
            projectUser.setUser(user);
            projectUsers.add(projectUser);
            projectUserDao.save(projectUser);
            //通知
            noticeService.sendMessage(new Message(project.getPm(), user, "项目[" + project.getProjectName() + "]计划确认开始 , 请确认计划内容是否合理"));
        }
        //保存
        taskService.saveAll(tasks);
        project.setUsers(projectUsers);
        projectService.save(project);
    }


    /**
     * 用户确认项目
     *
     * @param project 项目
     * @param user    确认人
     */
    @Transactional
    public void confirmProject(Project project, User user , Task task) {
        ProjectUser projectUser = projectUserDao.getFirstByProjectAndUser(project, user);
        if (projectUser == null) {
            log.info("确认任务错误! , 用户{}不能被{}找到", project.getProjectName(), user.getRealName());
            return;
        }
        projectUser.setConfirm(true);
        projectUserDao.save(projectUser);

        task.setStatus(StatusEnum.FINISH);
        taskService.save(task);
        //查看其他参与人是否全部同意
        List<ProjectUser> projectUsers = projectService.getProjectUserByProject(project);
        boolean flag = true;
        for (ProjectUser pu : projectUsers) {
            if (!pu.isConfirm()) {
                flag = false;
            }
        }
        if (flag) {
            project.setStatus(StatusEnum.CHECKED);
            projectService.save(project);
            //给项目经理发送通知告知全部已经确认完毕
            noticeService.sendMessage(new Message(project.getPm() , project.getPm() , "项目["+project.getProjectName()+"]所有负责人已经确认完毕"));
        }
    }
}
